The WHERE Clause

In this lesson, we will learn how to use the WHERE clause in SQL to view specific data from table.

The WHERE clause#

The SQL WHERE clause is used to specify a condition while fetching the data from a single table. If the given condition is satisfied, then those specific records are returned from the table.

Syntax#

The basic syntax of the SELECT statement with the WHERE clause is as shown below:

SELECT column1, column2, ... columnN 

FROM table_name

WHERE [condition];

You can specify a condition using the comparison or logical operators like >, <, =, LIKE, NOT, etc.

Example #1#

Let’s consider the CUSTOMERS table again.

ID NAME AGE ADDRESS SALARY
1 Mark 32 Texas 50000.00
2 John 25 NY 65000.00
3 Emily 23 Ohio 20000.00
4 Bill 25 Chicago 75000.00
5 Tom 27 Washington 35000.00
6 Jane 22 Texas 45000.00

Let’s say we want to fetch the ID, Name and Salary fields from the CUSTOMERS table, provided that the salary of the customer is greater than $50,000.

So when we write our SQL query, will get the following result:

Created with Fabric.js 1.6.0-rc.1 The WHERE clause will select those records where the salary is greater than 50000
1 of 2

The following code shows how to do this in SQL:

Example #2#

Let’s consider another query, which would fetch all the fields from the CUSTOMERS table for a customer with the name John.

In this case, our query will produce the following result:

Created with Fabric.js 1.6.0-rc.1 The WHERE clause will select those records where the customer name is John
1 of 2

The SQL query is for this problem written below:

Here, it is important to note that all the strings and characters should be inside single quotes (’’), whereas, numeric values should be given without any quotes.

Quick quiz!#

Q

What will be the output of the following query?

SELECT NAME,  ADDRESS 
FROM CUSTOMERS
WHERE ADDRESS = 'Texas';
A)
NAME ADDRESS
Mark Texas
John NY
Emily Ohio
Bill Chicago
Tom Washington
Jane Texas
B)
NAME   ADDRESS
Mark Texas
C)
NAME   ADDRESS
Jane Texas
D)
NAME  ADDRESS
Mark Texas
Jane Texas

In the next lesson, we will take a look a the AND & OR clause.

The SELECT Clause
The AND & OR Clauses
Mark as Completed
Report an Issue